using System;
using System.Collections.Generic;
using UnityEngine;

public class EventManager
{
    private static EventManager _instance;

    private Dictionary<string, Action<object>> eventDict;

    public static EventManager Instance
    {
        get {
            if (_instance == null)
            {
                _instance = new EventManager();
            }
            return _instance;
        }
        
    }

    public delegate void EventHandler();
    public static event EventHandler Event;

    private EventManager()
    {
        Debug.Log("EventManager constructor");
        eventDict = new Dictionary<string, Action<object>>();
    }


    public void Sub(string eventName, Action<object> callback)
    {
        if (!eventDict.ContainsKey(eventName))
        {
            eventDict[eventName] = callback;
        }
        else
        {
            eventDict[eventName] += callback;
        }
    }

    public void UnSub(string eventName, Action<object> callback)
    {
        if (eventDict.ContainsKey(eventName))
        {
            eventDict[eventName] -= callback;
            if (eventDict[eventName] == null)
            {
                eventDict.Remove(eventName);
            }
        }
    }


    public void Pub(string eventName, object data = null)
    {
        if (eventDict.ContainsKey(eventName))
        {
            eventDict[eventName]?.Invoke(data);
        }
    }

}